/**
 * Create in 2012-4-8.
 */
package kit4j.util.image;

import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;

/**
 * Image图像工具。
 * 
 * @author MiniKnife
 *
 */
public class ImageUtil {

	public static void waitForImageLoading(Component component, Image img) {
		MediaTracker mediaTracker = new MediaTracker(component);
		
		mediaTracker.addImage(img, 0);
		
		waitForAllImagesFromMediaTracker(mediaTracker);
	}
	
	public static void waitForImagesLoading(Component component, Image...imgs) {
		MediaTracker mediaTracker = new MediaTracker(component);
		
		for(int i = 0; i < imgs.length; ++i) {
			mediaTracker.addImage(imgs[i], i);
		}
		
		waitForAllImagesFromMediaTracker(mediaTracker);
	}
	
	private static void waitForAllImagesFromMediaTracker(MediaTracker mediaTracker) {
		try {
			mediaTracker.waitForAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
